using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using ZR.Service.BaseFrame.IService;
using ZR.Model.BaseFrame.Dto;
using ZR.Model.BaseFrame.Entity;
using ZR.Model;
using ZR.Admin.WebApi.MainModules;
using ZR.ServiceCore.Attributes;

namespace ZR.Admin.WebApi.Controllers.BaseFrame
{
    /// <summary>
    /// 数据字典
    /// </summary>
    public class DataDicControllerBase : ApiControllerBase
    {

        /// <summary>
        /// 数据字典接口
        /// </summary>
        protected readonly IDataDicService _DataDicService;

        protected readonly MapsterAdaptEntityCfg _mapsterAdaptEntityCfg;

        public DataDicControllerBase(IDataDicService dataDicService, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
        {
            _DataDicService = dataDicService;
            _mapsterAdaptEntityCfg = mapsterAdaptEntityCfg;
        }

        /// <summary>
        /// 添加数据字典
        /// </summary>
        /// <returns></returns>
        [HttpPost("add")]
        [ActionPermissionFilter(Permission = "datadic:add")]
        [Log(Title = "数据字典", BusinessType = BusinessType.INSERT)]
        public virtual async Task AddDataDic([FromBody] DataDicFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<DataDicEntity>(_mapsterAdaptEntityCfg).ToCreate(HttpContext, EntityExtension.AutoFillField2);
            var isOk = await _DataDicService.AddDataDic(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "添加失败");
            }
        }

        /// <summary>
        /// 修改数据字典
        /// </summary>
        /// <returns></returns>
        [HttpPost("modify")]
        [ActionPermissionFilter(Permission = "datadic:edit")]
        [Log(Title = "数据字典", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ModifyDataDic([FromBody] DataDicFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<DataDicEntity>(_mapsterAdaptEntityCfg).ToUpdate(HttpContext);
            var isOk = await _DataDicService.ModifyDataDic(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }

        /// <summary>
        /// 删除数据字典
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "datadic:delete")]
        [Log(Title = "数据字典", BusinessType = BusinessType.DELETE)]
        public virtual async Task DeleteDataDic([FromBody] DeleteDataDicReq req)
        {
            var idArr = Tools.SplitAndConvert<long>(req.Ids);
            var isOk = await _DataDicService.DeleteDataDic(idArr, false);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "删除失败");
            }
        }

        /// <summary>
        /// 变更数据字典状态
        /// </summary>
        /// <returns></returns>
        [HttpPost("changeState")]
        [ActionPermissionFilter(Permission = "datadic:changestate")]
        [Log(Title = "数据字典", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ChangeDataDicState([FromBody] ChangeDataDicStateReq req)
        {
            var isOk = await _DataDicService.ChangeDataDicState(req);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }




        /// <summary>
        /// 查询数据字典树选择列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("treeSelectList")]
        [ActionPermissionFilter(Permission = "datadic:treeSelectList")]
        public virtual async Task<List<TreeNodeInfo>> GetDataDicTreeSelectList([FromBody] GetDataDicTreeSelectListReq req)
        {
            var result = await _DataDicService.GetDataDicTreeSelectList(req);
            return result;
        }


        /// <summary>
        /// 查询数据字典分页列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("pageList")]
        [ActionPermissionFilter(Permission = "datadic:list")]
        public virtual async Task<PagedInfo<DataDicListItemDto>> GetDataDicPageList([FromBody] GetDataDicPageListReq req)
        {
            var result = await _DataDicService.GetDataDicPageList(req);
            return result;
        }

        /// <summary>
        /// 查询数据字典详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [ActionPermissionFilter(Permission = "datadic:detail")]
        public virtual async Task<DataDicDetailDto> GetDataDicDetail(long id)
        {
            var result = await _DataDicService.GetDataDicDetail(id);
            return result;
        }

    }
}